package org.wuyichun.course.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;
import org.wuyichun.base.service.impl.BaseServiceImpl;
import org.wuyichun.base.util.BaiduAuditUtils;
import org.wuyichun.course.domain.Comment;
import org.wuyichun.course.domain.Reply;
import org.wuyichun.course.service.ICommentService;

import java.util.*;
@Service
public class CommentServiceImpl extends BaseServiceImpl<Comment> implements ICommentService {
    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public void add(Comment comment) {
        // 审核
        Map<String, Object> map = BaiduAuditUtils.textCensor(comment.getContent());
        Boolean success = (Boolean)map.get("success");
        if (!success){
            throw new RuntimeException("非法评论");
        }
        // 审核记录表
        comment.setId(UUID.randomUUID().toString().replace("-",""));
        comment.setType(1);
        comment.setCreateTime(new Date());
        mongoTemplate.insert(comment);
    }

    @Override
    public List<Comment> getCommentByCourseId(Long id) {
        Query query = Query.query(Criteria.where("courseId").is(id));
        List<Comment> comments = mongoTemplate.find(query, Comment.class);
        return comments;
    }

    @Override
    public void replyComment(Map<String, Object> map) {
        // 获取评论的id
        String commentId = (String)map.get("commentId");
        // 获取回复的内容
        String content = (String)map.get("content");

        // 审核
        Map<String, Object> map1 = BaiduAuditUtils.textCensor(content);
        Boolean success = (Boolean)map1.get("success");
        if (!success){
            throw new RuntimeException("非法评论");
        }

        // 获取用户信息
        Long userId = Long.parseLong(map.get("userId").toString());
        String userName = (String)map.get("userName");

        Query query = Query.query(Criteria.where("id").is(commentId));
        Comment comment = mongoTemplate.findOne(query, Comment.class);
        List<Reply> replies = comment.getReplies();
        // 如果是第一此回复
        if (Objects.isNull(replies)){
            replies = new ArrayList<>();
        }
        // 封装回复对象
        Reply reply = new Reply();
        reply.setContent(content);
        reply.setCreateTime(new Date());
        reply.setId(UUID.randomUUID().toString().replace("-",""));
        reply.setUserId(userId);
        reply.setUserName(userName);

        // 将新的回复 +入评论中去
        replies.add(reply);

        Update replies1 = new Update().set("replies", replies);
        // 更新mongodb的内容
        mongoTemplate.updateFirst(query,replies1,Comment.class);
    }

    @Override
    public void deleteComment(Long id) {
        Query query = Query.query(Criteria.where("commentId").is(id));
        mongoTemplate.remove(query,Comment.class);
    }

    @Override
    public void deleteReply(Map<String, Object> map) {
        String commentId = map.get("commentId").toString();
        String replyId = map.get("replyId").toString();
        Query query = Query.query(Criteria.where("id").is(commentId));
        Comment comment = mongoTemplate.findOne(query, Comment.class);
        List<Reply> replies = comment.getReplies();
        for (Reply reply:replies) {
            if (reply.getId().equals(replyId)){
                Query query1 = Query.query(Criteria.where("id").is(replyId));
                mongoTemplate.remove(query1,Reply.class);
            }
        }
    }
}
